//
//  CXChannelInterActionController.m
//  ProjectOfBStage
//
//  Created by cuixin on 15/11/13.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "CXChannelInterActionController.h"
#import "CustomerNetworking.h"
#import "CXFirstTableViewCell.h"
#import "CXInterActionFirstModel.h"
#import "CXFisrtActionColelctionCell.h"
#import "CXSecondCollectionViewCell.h"
#import "CXSecondTableViewCell.h"
#import "CXInterSecondModel.h"
#import "CXThirdModel.h"
#import "ChannerModel.h"
#import "Reachability.h"
#import "CXThirdCollectionViewCell.h"
#import "CXThirdTableViewCell.h"
#import "CXDetailFirstViewController.h"
#import <MJRefresh.h>
#import "CXNewChannelCollectionCell.h"

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface CXChannelInterActionController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *firstArray;
@property(nonatomic, retain)NSMutableArray *secondArray;
@property(nonatomic, retain)NSMutableArray *thirdArray;
//@property(nonatomic, retain)UICollectionView *collectionView;

@property(nonatomic, retain)CXFirstTableViewCell *cell;

@property(nonatomic, retain)CXSecondTableViewCell *secondCell;

@property(nonatomic, retain)CXThirdTableViewCell *thirdCell;

@property(nonatomic, retain)UICollectionView *secondCollectionView;

@property(nonatomic, assign)NSInteger page;

@property(nonatomic, assign)BOOL loading;

//@property(nonatomic, retain)NSMutableArray *secondArray;

@end

@implementation CXChannelInterActionController
-(void)dealloc{
    [_firstArray release];
    [_secondArray release];
    [_thirdArray release];
    [_tableView release];
//    [_collectionView release];
    [_cell release];
    [_secondCell release];
    [_thirdCell release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [CustomerNetworking customerWithURL:@"http://echosystem.kibey.com/feed/mine?user_id=5549581&page=1" andParamer:nil andBlock:^(id response) {
//        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"tvs"]) {
//            CXThirdModel *model = [CXThirdModel modelWithDictionary:dic];
//            [self.firstArray addObject:model];
//        }
//        NSLog(@"!!!!!!!!!!%@", self.firstArray);
////        [self.tableView reloadData];
//    }];
//
//    
//    
//    self.automaticallyAdjustsScrollViewInsets = NO;
//
    self.secondArray = [NSMutableArray array];
//    self.thirdArray = [NSMutableArray array];
//    self.firstArray = [NSMutableArray array];
//    
//    [self loadThirdData];
//    [self loadData];
//    [self loadSecondData];
//
//    
//    [self createTableView];
    
    
    [self createSecondCollectionView];
    [self secondHeader];
    [self secondFooter];

}
-(void)secondHeader{
    self.secondCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        self.loading =NO;
        [self loadNewData:self.page andLoading:NO];
        
    }];
    [self.secondCollectionView.header beginRefreshing];
}
-(void)secondFooter{
    self.secondCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        self.loading = YES;
        [self loadNewData:self.page andLoading:YES];
    }];
}
#pragma mark 解析最热数据
-(void)loadNewData:(NSInteger)page andLoading:(BOOL)loading{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *documentPath = [path stringByAppendingPathComponent:@"CXSaveHot.plist"];
    if ([self haveNetWork]) {
        
    [CustomerNetworking customerWithURL:[NSString stringWithFormat:@"http://echosystem.kibey.com/channel/get?with_sound=0&order=new&page=%ld", page] andParamer:nil andBlock:^(id response) {
        if (loading == NO) {
            [self.secondArray removeAllObjects];
        }
        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"data"]) {
            ChannerModel *model = [ChannerModel modelWithDictionary:dic];
            [self.secondArray addObject:model];
        }
        [self.secondCollectionView reloadData];
        [self.secondCollectionView.header endRefreshing];
        [self.secondCollectionView.footer endRefreshing];
        self.secondCollectionView.header.hidden = YES;
        [NSKeyedArchiver archiveRootObject:self.secondArray toFile:documentPath];

        
    }];
    }
    if ([self haveNetWork] == NO) {
        NSMutableArray *tempAray = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
        self.secondArray = tempAray;
        [self.secondCollectionView.header endRefreshing];
        [self.secondCollectionView.footer endRefreshing];
        [self.secondCollectionView reloadData];
    }

}
-(void)createSecondCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.secondCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, WIDTH_PRO(10), kWidth, kHeight -  HEIGHT_PRO(90)) collectionViewLayout:flowLayout];
    self.secondCollectionView.backgroundColor = [UIColor whiteColor];
    self.secondCollectionView.delegate = self;
    self.secondCollectionView.dataSource = self;
    //注册cell
    [self.secondCollectionView registerClass:[CXNewChannelCollectionCell class] forCellWithReuseIdentifier:@"CXNewChannelCollectionCellID"];
    [self.view addSubview:self.secondCollectionView];
    [_secondCollectionView release];
    [flowLayout release];
}



- (BOOL)haveNetWork
{
    Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    switch ([reachability currentReachabilityStatus]) {
        case NotReachable:
            NSLog(@"没有网络连接");
            return NO;
            break;
        case ReachableViaWWAN:
            // 使用3G网络
            //            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"号码不可用\n或您的备设不支持拨号功能" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            //            [alertView show];
            return YES;
            break;
        case ReachableViaWiFi:
            // 使用WIFI
            NSLog(@"有网络连接");
            return YES;
        default:
            break;
    }
}



//-(void)createTableView{
//    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 60) style:UITableViewStylePlain];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    [self.view addSubview:self.tableView];
//}
//#pragma mark 解析相关频道数据
//-(void)loadData{
//    [CustomerNetworking customerWithURL:@"http://echosystem.kibey.com/echofamous/tv/tvs" andParamer:nil andBlock:^(id response) {
//                for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"channels"]) {
//                    CXInterActionFirstModel *model = [CXInterActionFirstModel modelWithDictionary:dic];
//                    [self.thirdArray addObject:model];
//                }
//        [self.tableView reloadData];
//            }];
//
//}
//
//#pragma mark 解析第二个cell数据
//-(void)loadSecondData{
//    [CustomerNetworking customerWithURL:@"http://echosystem.kibey.com/echofamous/tv/tvs" andParamer:nil andBlock:^(id response) {
//        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"users"]) {
//            CXInterSecondModel *model = [CXInterSecondModel modelWithDictionary:dic];
//            [self.secondArray addObject:model];
//        }
//        [self.tableView reloadData];
//        
//    }];
//}
//
//#pragma mark 解析第三种cell数据
//-(void)loadThirdData{
//    [CustomerNetworking customerWithURL:@"http://echosystem.kibey.com/echofamous/tv/tvs" andParamer:nil andBlock:^(id response) {
//        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"tvs"]) {
//            CXThirdModel *model = [CXThirdModel modelWithDictionary:dic];
//            [self.firstArray addObject:model];
//        }
//        [self.tableView reloadData];
//    }];
//}
//
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return 3;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.row == 0) {
//        return 130;
//
//    }if (indexPath.row == 1) {
//        return 130;
//
//    }
//    if (indexPath.row == 2) {
//        return 300;
//
//    }
//else{
//        return 130;
//    }
//}
//
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row == 0) {
//        static NSString *cellID = @"CXFirstTableViewCellID";
//        _cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//        if (!_cell) {
//          _cell = [[CXFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//        }
//        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 10)];
//        [lable setTextColor:[UIColor grayColor]];
//        [lable setText:@"相关频道"];
//        [lable setFont:[UIFont systemFontOfSize:12]];
//        [_cell addSubview:lable];
//        [lable release];
//        
//    _cell.collectionView.frame = CGRectMake(0, 20, kWidth, 110);
//    _cell.collectionView.backgroundColor = [UIColor whiteColor];
//    
//    [_cell.collectionView setDataSource:self];
//    [_cell.collectionView setDelegate:self];
//    [_cell.collectionView reloadData];
//    [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        return _cell;
//    }if (indexPath.row == 1)
//     {
//        static NSString *secondID = @"CXSecondTableViewCellID";
//        _secondCell = [tableView dequeueReusableCellWithIdentifier:secondID];
//        if (!_secondCell) {
//            _secondCell = [[CXSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:secondID];
//        }
//        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 10)];
//        [lable setTextColor:[UIColor grayColor]];
//        [lable setText:@"好声音入围学院"];
//        [lable setFont:[UIFont systemFontOfSize:12]];
//        [_secondCell addSubview:lable];
//        [lable release];
//         
//        _secondCell.collectionView.frame = CGRectMake(0, 30, kWidth, 100);
//        _secondCell.collectionView.backgroundColor = [UIColor whiteColor];
//        [_secondCell.collectionView setDataSource:self];
//        [_secondCell.collectionView setDelegate:self];
//        [_secondCell.collectionView reloadData];
//         
//        [_secondCell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        return _secondCell;
//    }
//    else{
//        static NSString *cellid = @"CXThirdTableViewCellID";
//        _thirdCell = [tableView dequeueReusableCellWithIdentifier:cellid];
//        if (!_thirdCell) {
//            _thirdCell = [[CXThirdTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
//        }
//        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 10)];
//        [lable setTextColor:[UIColor grayColor]];
//        [lable setText:@"全部互动节目"];
//        [lable setFont:[UIFont systemFontOfSize:12]];
//        [_thirdCell addSubview:lable];
//        [lable release];
//        
//        _thirdCell.collectionView.frame = CGRectMake(0, 20, kWidth, 272);
//        _thirdCell.collectionView.backgroundColor = [UIColor whiteColor];
//        [_thirdCell.collectionView setDataSource:self];
//        [_thirdCell.collectionView setDelegate:self];
//        [_thirdCell.collectionView reloadData];
//        [_thirdCell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        return _thirdCell;
//    }
//}


#pragma mark collectionView的代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.secondCollectionView != 0) {
        if (collectionView == self.secondCollectionView) {
            return self.secondArray.count;
        }
    }
    return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
        CXNewChannelCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXNewChannelCollectionCellID" forIndexPath:indexPath];
        ChannerModel *model = [self.secondArray objectAtIndex:indexPath.row];
        cell.channelModel = model;
        return cell;

    
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return  HEIGHT_PRO(5);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(WIDTH_PRO(184), HEIGHT_PRO(120));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, HEIGHT_PRO(5), WIDTH_PRO(5));
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    

        CXDetailFirstViewController *detail = [[CXDetailFirstViewController alloc] init];
        ChannerModel *model = [self.secondArray objectAtIndex:indexPath.row];
        detail.channelModel = model;
        detail.bigImage = model.pic;
        detail.detailID = model.ID ;
        detail.name = model.name;
        detail.hidesBottomBarWhenPushed = YES;
        self.navigationController.navigationBar.hidden = YES;
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
